<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div id="content" class="col-lg-10 col-sm-10">
    <!-- content starts -->
    <div>
        <ul class="breadcrumb">
            <li>
                <a href="#">Home</a>
            </li>
            <li>
                <a href="#">faculty-assign-subjects</a>
            </li>
        </ul>
    </div>

    <div class="row">
        <div class="box col-md-12">
            <div class="box-inner">
                <form  action="FacultySubjectAssignAction.do" method="post">

                    <div class="box-header well" data-original-title="">
                        <h2>Filter Criteria</h2>
                    </div>
                    <div class="box-content">
                        <div class="control-group">
                            <div class="controls">
                                <input type="hidden" name="fid" value="${sessionScope.faculty.id}"/>
                                <label class="control-label" for="selectError">Select Branch</label>

                                <select id="branch_select" name="brid" data-rel="chosen">
                                    <option value="-1">None</option>
                                    <c:forEach items="${applicationScope.li_allbranches}" var="branch">
                                        <option value="${branch.id}">${branch.name}</option>    
                                    </c:forEach>

                                </select> 
                                <br><br>
                                <label class="control-label" for="selectError">Select Semester</label>

                                <select id="semester_select" name="semester" data-rel="chosen">
                                    <option value="-1">None</option>
                                    <c:forEach begin="1" end="8" var="i">
                                        <option  value="${i}">${i}</option>
                                    </c:forEach>
                                </select> 

                                <br><br>
                                <label class="control-label" for="selectError">Select Division</label>

                                <select id="division_select" name="divid"  >
                                    <option  value="-1">None</option>



                                </select>

                                <br><br>
                                <label class="control-label" for="selectError">Select Subjects :</label>
                                <select id="subject_select" name="subid" >
                                    <option value="-1">None</option>



                                </select>
                                <br><br>
                                <label class="control-label" for="selectError">Select No of lectures :</label>

                                <select id="lecture_select" name="nooflectures" >
                                    <option  value="-1">none</option>

                                </select> 
                                <br><br>
                                <label class="control-label" for="selectError">Select No of labs :</label>

                                <select id="lab_select" name="nooflabs" >
                                    <option  value="-1">none</option>

                                </select> 
                                <br><br>

                            </div>

                        </div>

                    </div>

                    <p class="center col-md-5">
                        <input type="submit" class="btn btn-primary" value="Add">
                    </p>

                </form>
            </div>

        </div>
    </div>

    <div class="row">
        <div class="box col-md-12">
            <div class="box-inner">
                <div class="box-header well" data-original-title="">
                    <h2>Subject Assigned to ${sessionScope.faculty.fname} &nbsp; ${sessionScope.faculty.lname}</h2>

                    <div class="box-icon">

                    </div>

                </div>

            </div>
            <div class="box-content">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Subject Name</th>
                            <th>Semester</th>
                            <th>Branch</th>
                            <th>Division</th>
                            <th>No of lectures</th>
                            <th>No of labs</th>
                            <th>Action</th>
                            <!--<th>Subject Name</th>
                                 <th>Id</th>
                                 <th>branch name</th>
                                 <th>admin name</th>-->
                        </tr>
                    </thead>
                    <tbody>
                        <c:forEach var="fs" items="${sessionScope.faculty_subjects }" varStatus="j">
                            <tr id="tr_${fs.id}">
                                <td>${fs.subject.name}</td>
                                <td>${fs.semester}</td>
                                <td>${fs.branch_obj.name}</td>
                                <td>${fs.division.name}</td>
                                <td>${fs.nooflectures}</td>
                                <td>${fs.nooflabs}</td>
                                <td> <button class="label-success label label-default delete_button" id="delete_${fs.id}">Delete</button></td>
                            </tr>
                        </c:forEach>

                    </tbody>

                </table>
            </div>
                    <br><br>
        </div>
    </div>
    <script>



//for get division
        // window.onload = function() {
        $("#semester_select").change(function() {
            //alert("Val = "+$("#semester_select").val());
            $.ajax({
                type: "POST",
                url: "FilterAjaxAction.do",
                data: "action=getDivisions&branch_id= " + $("#branch_select").val() + "&semester=" + $("#semester_select").val(),
                success: function(data) {
                    console.log(data);
                    data = JSON.parse(data);
                    var html = "";
                    $(data).each(function(index, division) {
                      
                        html += "<option value=" + division.id + ">" + division.name + "</option>";
                    });
                   
                    $("#division_select").append(html);


                }
            });
        });

//for getting subject
        $("#division_select").change(function() {
          
            $.ajax({
                type: "POST",
                url: "FacultyAssignSubjectAjax.do",
                data: "branch=" + $("#branch_select").val() + "&semester=" + $("#semester_select").val() + "&division=" + $("#division_select").val(),
                success: function(data) {
                    console.log(data);
                    data = JSON.parse(data);
                    var html = "";
                    if(data===null){
                        alert("all the subjects for that division has been already assigned");
                    }
                   alert(data);
                    $(data).each(function(index, subject) {
                       
                        
                        html += "<option value=" + subject.subid + ">" + subject.subject.name + "</option>";
                         
                    });
                   
                  $("#subject_select").append(html);

                }
            });
        });

//for getting lectures and labs for subjects
        $("#subject_select").change(function() {
            //alert("Val = "+$("#division_select").val());
            
            $.ajax({
                type: "POST",
                url: "FacultyAssignSubjectForLectureAjax.do",
                data: "subid=" + $("#subject_select").val() + "&division=" + $("#division_select").val(),
                success: function(data) {
                    console.log(data);
                    data = JSON.parse(data);
                    var html = "";
                    var html1 = "";

                    $(data).each(function(index, subject) {
                        var i = subject.lecturesperweek;
                         if(i===0){
                                html += "<option value=" + i + ">" + i + "</option>";
                                 }
                        $(i).each(function(index, i) {
                            while (i > 0) {
                                i--;
                            }
                            while (i <= subject.lecturesperweek) {
                                html += "<option value=" + i + ">" + i + "</option>";
                                i++;
                            }
                        }

                        );
                    });
                    $("#lecture_select").empty();
                    $("#lecture_select").append(html);


                    $(data).each(function(index, subject) {
                        var i = subject.labsperweek;
                        if(i===0){
                                html1 += "<option value=" + i + ">" + i + "</option>";
                                }
                        $(i).each(function(index, i) {
                           while (i > 0) {
                                i--;
                            }
                            while (i <= subject.labsperweek) {
                                html1 += "<option value=" + i + ">" + i + "</option>";
                                i++;
                                }
                        }
                        );
                    });
                     $("#lab_select").empty();
                    $("#lab_select").append(html1);
                }
            });
        });


        //  }
    </script>